﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.CarModel
{
    public partial class AddRepair : BaseForm
    {
        RepairSvc.SoapHeaderClass sopaHeader = new RepairSvc.SoapHeaderClass();

        private bool isDriver = false;
        private int RepId = 0;
        private string pageInfo = null;
        public AddRepair()
        {
            InitializeComponent();
        }
        public AddRepair(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }

        public AddRepair(bool isDri, int id, string edit)
        {
            InitializeComponent();
            isDriver = isDri;
            RepId = id;
            pageInfo = edit;
        }

        private void clear()
        {
            txtCarCode.Text = "";
            txtCents.Text = "";
            txtCLf.Text = "";
            txtMarks.Text = "";
            txtOtherPrice.Text = "";
            txtRGF.Text = "";
            txtSum.Text = "";
            txtUser.Text = "";
        }
        private void AddRepair_Load(object sender, EventArgs e)
        {
            txtUser.Text = Login.LogUserName.ToString();
            BindComboBoxInfo();
            if (RepId != 0)
            {
                BindInfo();
                btnCancel.Text = "返回";
            }
        }
        private void BindComboBoxInfo()
        {
            //BLL.Archives bll = new BLL.Archives();
            //DataSet ds = bll.ComBoxCompany();
            //if (ds != null)
            //{
            //    cmbFactory.DisplayMember = "Name";
            //    cmbFactory.ValueMember = "Id";
            //    cmbFactory.DataSource = ds.Tables[0];
            //    cmbFactory.SelectedIndex = 0;

            //}

            //DataSet  = bll.ComBoxMakeType();
            //if (dsMakeType != null)
            //{
            //    cmbType.DisplayMember = "Name";
            //    cmbType.ValueMember = "Id";
            //    cmbType.DataSource = dsMakeType.Tables[0];
            //    cmbType.SelectedIndex = 0;
            //}

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ArchivesSvc.SoapHeaderClass sopaHeader = new ArchivesSvc.SoapHeaderClass();

            DataSet dsMT = null;

            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxCompany(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsMT = (DataSet)result.Result;
                        cmbFactory.DisplayMember = "Namevalue";
                        cmbFactory.ValueMember = "Id";
                        cmbFactory.DataSource = dsMT.Tables[0];
                        cmbFactory.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

            DataSet dsMakeType = null;
            ServiceManipulator.SafeAsyncExecution<ArchivesSvc.ArchivesServiceSoapClient>(
                proxy => proxy.ComBoxMakeType(sopaHeader),
                result =>
                {
                    if (result.Error == null)
                    {
                        dsMakeType = (DataSet)result.Result;
                        cmbType.DisplayMember = "Namevalue";
                        cmbType.ValueMember = "Id";
                        cmbType.DataSource = dsMakeType.Tables[0];
                        cmbType.SelectedIndex = 0;

                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }
        private void BindInfo()
        {
            //BLL.Repair bll = new BLL.Repair();
            //Model.Repair model = bll.GetModel(RepId);


            //BLL.Accounts_Users userbll = new BLL.Accounts_Users();
            //Model.Accounts_Users usermodel = userbll.GetModel(model.InputUserId);
            //if (usermodel != null)
            //{
            //    txtUser.Text = usermodel.TrueName.ToString();
            //}

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            RepairSvc.Repair model = new RepairSvc.Repair();
            ServiceManipulator.SafeAsyncExecution<RepairSvc.RepairServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeader, RepId),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (RepairSvc.Repair)result.Result;
                        txtCarCode.Text = model.CarCode.ToString();
                        txtCents.Text = model.Cents.ToString();
                        txtCLf.Text = model.CLf.ToString();

                        txtMarks.Text = model.Marks.ToString();
                        txtOtherPrice.Text = model.OtherPrice.ToString();
                        txtRGF.Text = model.RGF.ToString();
                        txtSum.Text = model.Sum.ToString();
                        txttime.Value = model.Time;

                        cmbFactory.Text = model.Factory.ToString();
                        cmbType.Text = model.Type.ToString();
                        txtUser.Text = model.InputTrueName;
                        ckbIsRemind.Checked = model.isRemind;
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
            if (pageInfo == "look")
            {
                txtCarCode.Enabled = false; txtCents.Enabled = false;
                txtCLf.Enabled = false; txtMarks.Enabled = false;
                txtOtherPrice.Enabled = false; txtRGF.Enabled = false;
                txtSum.Enabled = false; txttime.Enabled = false;
                txtUser.Enabled = false; btnSave.Visible = false;
                cmbFactory.Enabled = false;
                cmbType.Enabled = false;
                ckbIsRemind.Enabled = false;
            }
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCarCode.Text))
            {
                DialogWarning dia = new DialogWarning("车辆编号不能为空！");
                dia.ShowDialog();
                return;
            }

            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            if (string.IsNullOrEmpty(txtCLf.Text))
            {
                txtCLf.Text = "0";
            }
            if (string.IsNullOrEmpty(txtRGF.Text))
            {
                txtRGF.Text = "0";
            }
            if (string.IsNullOrEmpty(txtOtherPrice.Text))
            {
                txtOtherPrice.Text = "0";
            }

            //BLL.Repair bll = new BLL.Repair();
            RepairSvc.Repair model = new RepairSvc.Repair();

            model.Time = txttime.Value;
            model.Cents = txtCents.Text;
            model.Factory = cmbFactory.Text;
            model.CLf = decimal.Parse(txtCLf.Text);
            model.RGF = decimal.Parse(txtRGF.Text);
            model.OtherPrice = decimal.Parse(txtOtherPrice.Text);
            model.Sum = decimal.Parse(txtCLf.Text) + decimal.Parse(txtRGF.Text) + decimal.Parse(txtOtherPrice.Text);
            model.Marks = txtMarks.Text;
            model.InputUserId = Login.LogUserId;
            model.Type = cmbType.Text;
            model.CarCode = txtCarCode.Text;
            model.InputTime = DateTime.Now;
            model.isRemind = ckbIsRemind.Checked;

            if (RepId > 0)
            {
                model.Id = RepId;
                //bll.Update(model);
                //DialogNotify diaSecc = new DialogNotify("维修信息修改成功！");
                //diaSecc.ShowDialog(); this.Close();

                ServiceManipulator.SafeAsyncExecution<RepairSvc.RepairServiceSoapClient>(
               proxy => proxy.Update(sopaHeader, model),
               result =>
               {
                   if (result.Error == null)
                   {
                       PageCommon.Notify("维修信息修改成功！");
                       overlay.FadeOut();Cursor.Current = Cursors.Default;
                   }
                   else
                   {
                       PageCommon.Error(result.Error.Message);
                   }
               },
               this
           );

            }
            else
            {
                //bll.Add(model);
                //DialogNotify diaSecc = new DialogNotify("维修信息添加成功！");
                //diaSecc.ShowDialog();

                ServiceManipulator.SafeAsyncExecution<RepairSvc.RepairServiceSoapClient>(
               proxy => proxy.Add(sopaHeader, model),
               result =>
               {
                   if (result.Error == null)
                   {
                       PageCommon.Notify("维修信息添加成功！");
                       overlay.FadeOut();Cursor.Current = Cursors.Default;
                   }
                   else
                   {
                       PageCommon.Error(result.Error.Message);
                   }
               },
               this
           );
            }
            clear();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
        private bool IsNumberMoney(System.Windows.Forms.TextBox t, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' || e.KeyChar == 8 || e.KeyChar == 13))
            {
                //lb.Text=string.Empty;
                return true;
            }
            else
            {
                DialogNotify diaSecc = new DialogNotify("只能输入数字！");
                diaSecc.ShowDialog();
                e.Handled = true;
                return false;
            }
        }
        private void txtCLf_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtCLf, e);
        }

        private void txtRGF_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtRGF, e);
        }

        private void txtOtherPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtOtherPrice, e);
        }

        private void txtOtherPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtSum.Text = (decimal.Parse(txtCLf.Text) + decimal.Parse(txtRGF.Text) + decimal.Parse(txtOtherPrice.Text)).ToString();
            }
        }

        private void txtRGF_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                txtSum.Text = (decimal.Parse(txtCLf.Text) + decimal.Parse(txtRGF.Text) + decimal.Parse(txtOtherPrice.Text)).ToString();
            }
        }

        #region 车牌号 输入框数据绑定

        string driverName = string.Empty;
        private void txtCarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = this.txtCarCode.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("车牌号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "车牌号");
                }
            }
        }

        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "车牌号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            DataSet ds = null;
            this.dgvDriverInfo.DataSource = null;
            dgvDriverInfo.AutoGenerateColumns = false;
            CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvDriverInfo.DataSource = ds.Tables[0];
                            this.dgvDriverInfo.Visible = true;
                            this.dgvDriverInfo.Focus();
                        }
                        else
                        {
                            PageCommon.Alert("查找的车辆不存在，请重新查询！");
                            txtCarCode.Text = driverName;
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvDriveInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvDriverInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvDriverInfo.Rows.Count; )
                {
                    this.dgvDriverInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                this.txtCarCode.Text = this.dgvDriverInfo.CurrentRow.Cells["车牌号1"].Value.ToString();
                //  driverID = Convert.ToInt32(this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString());
                driverName = this.txtCarCode.Text;
                this.dgvDriverInfo.Visible = false;
                this.txttime.Focus();
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvDriverInfo.Columns[e.ColumnIndex].Name == "性别")
            {
                if (e.Value != null)
                {
                    if (Convert.ToBoolean(e.Value))
                    {
                        e.Value = "带驾";
                    }
                    else
                    {
                        e.Value = "自驾";
                    }
                }
            }

        }


        #endregion 车牌号 输入框数据绑定

    }
}
